package com.xingdata.jjxc.m.login.avt;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.activity.AbActivity;
import com.ab.http.AbHttpUtil;
import com.ab.http.AbRequestParams;
import com.ab.http.AbStringHttpResponseListener;
import com.alibaba.fastjson.JSON;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.enty.UserEntity;
import com.xingdata.jjxc.m.avt.HUDWebView;
import com.xingdata.jjxc.utils.Code;
import com.xingdata.jjxc.utils.ExitApplication;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.SP;
import com.xingdata.jjxc.views.Widget;

public class ForgetPassword extends AbActivity implements OnClickListener {
	
	final static String TAG = "ForgetPassword";

	private TextView registerone_phone, registerone_code;
	private Button registerone_next;
	private TextView registerone_agreement;
	private TextView registerone_get_code;

	private RelativeLayout tip_register;
	private String keyword = "";
	/**
	 * 【HTTP请求】声明Http请求实例，并声明参数实例
	 */
	protected AbHttpUtil mAbHttpUtil = null;
	protected AbRequestParams params = null;
	protected RespEntity resp = null;

	// protected UserEntity user;
	// protected String[] info;

	private RelativeLayout icon_l;
	private TextView title;

	private Dialog register_exit;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.forget_password);
		// ExitApplication.getInstance().addActivity(this);
		// 获取Http工具类
		mAbHttpUtil = AbHttpUtil.getInstance(this);
		mAbHttpUtil.setDebug(true);
		mAbHttpUtil.setTimeout(15000);
		//
		// user = SP.getUserInfo(this);
		// info = SP.getMobileAndPassword(this);

		initViews();

		// ExitApplication.getInstance().addActivity(this);
	}

	private void initViews() {
		icon_l = (RelativeLayout) findViewById(R.id.icon_l);
		icon_l.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				register_exit();
			}
		});
		title = (TextView) findViewById(R.id.title);
		title.setText("忘记密码");

		tip_register = (RelativeLayout) findViewById(R.id.tip_register);
		tip_register.setOnClickListener(this);
		registerone_phone = (TextView) findViewById(R.id.registerone_phone_et);
		registerone_code = (TextView) findViewById(R.id.registerone_code_et);
		registerone_get_code = (TextView) findViewById(R.id.registerone_code_tv);
		registerone_get_code.setOnClickListener(this);

		registerone_next = (Button) findViewById(R.id.registerone_next_btn);
		registerone_next.setOnClickListener(this);

		registerone_agreement = (TextView) findViewById(R.id.registerone_agreement);
		registerone_agreement.setOnClickListener(this);
	}

	@SuppressLint("DefaultLocale")
	@Override
	public void onClick(View v) {
		String phone = registerone_phone.getText().toString();
		String recode = registerone_code.getText().toString();
		switch (v.getId()) {
		case R.id.registerone_next_btn:
			if (phone.equals("") || (phone.length() != 11)) {
				showToast(R.string.error_phone_num);
			} else if (recode.length() < 6) {
				showToast(R.string.error_val_code);
			} else {
				checkCode(phone, recode);
			}
			break;

		case R.id.tip_register:
			if (phone.equals("") || (phone.length() != 11)) {
				showToast(R.string.error_phone_num);
			} else {
				String phpwd = phone.substring(7, 11) + phone.substring(0, 7);
				keyword = Code.CodeMD5(Code.CodeMD5(phpwd).toLowerCase()).toLowerCase();
				sendCode(phone, keyword);
			}
			break;
		case R.id.registerone_agreement:
			Intent intent = new Intent(this, HUDWebView.class);
			Bundle bundle = new Bundle();
			bundle.putString("URL", App.JJ_AGREEMENT);
			intent.putExtras(bundle);
			startActivity(intent);
			break;

		default:
			break;
		}
		Widget.startActivityAnim(this, R.anim.avty_slide_in_from_right, R.anim.avty_slide_out_from_left);
	}

	private void sendCode(String phone, String keyword) {

		if (isNetworkConnected() == false) {
			showToast(R.string.network);
			return;
		}
		resp = null;
		params = new AbRequestParams();
		params.put("mobile", phone);
		params.put("keyword", keyword);

		mAbHttpUtil.post(App.ZZD_REQUEST_SENDCODE, params, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {
				resp = JSON.parseObject(content, RespEntity.class);
				new MyCountTimer(180, 1000).start();
			}

			@Override
			public void onStart() {
				showProgressDialog();
			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
			}

			@Override
			public void onFinish() {
				if (resp != null) {
					if (resp.getState() == 0) {
						showToast(R.string.send_code_success);
					} else if (resp.getState() == 1) {
						showToast(resp.getMsg());
					} else {
						showToast(resp.getMsg());
					}
					removeProgressDialog();
				} else {
					showToast("网络超时，请重试");
					removeProgressDialog();
					return;
				}
			}
		});
	}

	private void checkCode(final String phone, final String code) {
		if (isNetworkConnected() == false) {
			showToast(R.string.network);
			return;
		}
		resp = null;
		params = new AbRequestParams();
		params.put("mobile", phone);
		params.put("checknum", code);
		mAbHttpUtil.post(App.ZZD_REQUEST_CHECKCODE, params, new AbStringHttpResponseListener() {

			@Override
			public void onSuccess(int statusCode, String content) {
				resp = JSON.parseObject(content, RespEntity.class);
			}

			@Override
			public void onStart() {
				showProgressDialog();
			}

			@Override
			public void onFailure(int statusCode, String content, Throwable error) {
			}

			@Override
			public void onFinish() {
				if (resp != null) {
					if (resp.getState() == 0) {
						Intent intent = new Intent(ForgetPassword.this, Register2.class);
						Bundle bundle = new Bundle();
						bundle.putString("phone", phone);
						bundle.putString("checknum", code);
						intent.putExtras(bundle);
						startActivity(intent);
						Widget.startActivityAnim(ForgetPassword.this, R.anim.avty_slide_in_from_right,
								R.anim.avty_slide_out_from_left);
						finish();
					} else if (resp.getState() == 1) {
						showToast(resp.getMsg());
					} else {
						showToast(resp.getMsg());
					}
					removeProgressDialog();
				} else {
					showToast("网络超时，请重试");
					removeProgressDialog();
					return;
				}
			}

		});
	}

	class MyCountTimer extends CountDownTimer {

		public MyCountTimer(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
			registerone_get_code.setText("获取验证码");
			registerone_get_code.setClickable(true);
		}

		@Override
		public void onTick(long millisUntilFinished) {
			registerone_get_code.setText(String.valueOf(millisUntilFinished) + "秒后重新获取");
			registerone_get_code.setClickable(false);
		}

	}

	protected boolean isNetworkConnected() {

		ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);

		cwjManager.getActiveNetworkInfo();
		boolean netSataus = true;
		if (cwjManager.getActiveNetworkInfo() == null) {

			netSataus = false;
		}
		return netSataus;
	}

	@SuppressLint("InflateParams")
	private void register_exit() {
		Log.d(TAG, "register_exit()");
		// 修改字体
		Typeface font = Typeface.createFromAsset(this.getAssets(), JUtils.TypefacePath);
		if (register_exit == null) {
			register_exit = new Dialog(this, R.style.my_dialog);
			View v = LayoutInflater.from(this).inflate(R.layout.pop_register, null);
			LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
			register_exit.addContentView(v, params);

			View blank = v.findViewById(R.id.register_view);
			
			TextView exit_textView = (TextView) v.findViewById(R.id.exit_TextView);
			exit_textView.setTypeface(font);

			Button exit = (Button) v.findViewById(R.id.register_exit);
			exit.setTypeface(font);
			Button cancel = (Button) v.findViewById(R.id.register_no);
			cancel.setTypeface(font);
			
			cancel.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					register_exit.dismiss();
				}
			});
			exit.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View arg0) {

					register_exit.dismiss();
					startActivity(new Intent(ForgetPassword.this, LoginActivity.class));
					Widget.startActivityAnim(ForgetPassword.this, R.anim.avty_slide_in_from_right,
							R.anim.avty_slide_out_from_left);
					ForgetPassword.this.finish();
				}
			});

			blank.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					register_exit.dismiss();
				}
			});

		}
		if (!this.isFinishing() && register_exit != null && !register_exit.isShowing()) {
			register_exit.show();
		}
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			register_exit();
		}
		return false;
	}

}
